<?php

/*
*/

class Sale_Advice_Stock_model extends CI_Model
{

	static $TABLE;
	public $error;

	function __construct()
	{
		parent::__construct();

		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();

		//载入核心类
		$this->load->database($this->test_database->get_db());

		//载入模型文件
		$this->load->model('sub_goods_model');

		//载入库文件
		$this->load->library('form_validation');

		//载入本地语言文件
		$this->lang->load('form_validation','chinese');

		//初始化数据
		$this->TABLE = 'sale_advices_stocks';		//数据表
		$this->TEMP_TABLE = 'temp_' . $this->TABLE;	//临时数据表
		$this->error = FALSE;		//错误
		$this->mode = FALSE;		//处理模式

		//初始化容器
		$this->sub_goods = array();	//子货物信息
	}

	/***
	For private function here
	***/

	/***
	For public function here
	***/

	public function create_stocks($sale_advice_id, $sub_goods_quantity, $sub_goods_return_quantity)
	{

		//获得当前所有子货物数据
		$this->sub_goods = $this->sub_goods_model->get_all();
		//获得所有机构和仓库
		$this->store_ids = array(1, 2);

		//遍历所有子货物
		foreach ($this->sub_goods as $sub_goods_id=>$goods)
		{
			//遍历所有仓库
			foreach ($this->store_ids as $store_id)
			{
				//判断子货物在指定当前是否销售过
				if (isset($sub_goods_quantity[$goods->sub_goods_id][$store_id]))
				{
					$quantity = $sub_goods_quantity[$goods->sub_goods_id][$store_id];
					$return_quantity = $sub_goods_return_quantity[$goods->sub_goods_id][$store_id];
				}
				else
				{
					$quantity = 0;
					$return_quantity = 0;
				}

				//创建单条销售库存结算数据
				if (FALSE === $this->create_single_stock($sale_advice_id, $goods->sub_goods_id, $store_id, $quantity, $return_quantity))
				{
					return FALSE;
				}
			}
		}

		return TRUE;

	}

	public function create_single_stock($sale_advice_id, $sub_goods_id, $store_id, $quantity, $return_quantity)
	{
		try {
			$data = array(
				'sale_advice_id'	=> $sale_advice_id,
				'sub_goods_id'		=> $sub_goods_id,
				'store_id'			=> $store_id,
				'quantity'			=> $quantity,
				'return_quantity'	=> $return_quantity,
				'create_date'		=> time()
				);
			if ($this->db->insert($this->TABLE, $data))
			{
				return TRUE;
			}
			else
			{
<<<<<<< HEAD
				echo $this->db->last_query();
=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
				$this->error = '创建单条销售报表库存结算数据失败 SUB_GOODS_ID:' . $sub_goods_id;
				return FALSE;
			}
		} catch (Exception $e) {
			$this->error = $e;
			return FALSE;
		}
	}

	//$in_key[boolean] : 将sub_goods_id存入数组key中
	function get_stock($sale_advice_id, $id_in_key=FALSE)
	{
		$advice_stocks = array();	//初始化容器

		$this->db->where('sale_advice_id = ' . $sale_advice_id);
		$query = $this->db->get($this->TABLE);

		if ($query->num_rows() > 0)
		{
			$advice_stocks = $query->result();
		}

		if ($id_in_key)
		{
			$advice_stocks_ = array();	//初始化新的容器
			foreach ($advice_stocks as $stock)
			{
				if (!isset($advice_stocks_[$stock->sub_goods_id][$stock->store_id]))
				{
					$advice_stocks_[$stock->sub_goods_id][$stock->store_id]['sale_quantity'] = $stock->quantity;
					$advice_stocks_[$stock->sub_goods_id][$stock->store_id]['return_quantity'] = $stock->return_quantity;
				}
			}
			return $advice_stocks_;
		}
		else
		{
			return $advice_stocks;
		}
	}

}